﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Iacym.BusinessLogic;
using Iacym.Entities;
using Microsoft.Practices.Unity;

namespace Iacym.UI
{
    public partial class FrmUbigeo : Form
    {
        [Dependency]
        public IUbigeoBL UbigeoBL { get; set; }

        [Dependency]
        public IUsuarioMenuBL usuMenuBL { get; set; }

        public FrmUbigeo()
        {
            InitializeComponent();
            this.BuildUp();
        }

        bool isExtend;

        public void Extend()
        {
            isExtend = true;
        }

        public USUARIO currentUsuarioUbigeo { get; set; }

        public UBIGEO _currentUbigeo
        {
            get { return uBIGEOBindingSource.Current as UBIGEO; }
            set { uBIGEOBindingSource.DataSource = value; }
        }

        private void frmListUbigeo_Load(object sender, EventArgs e)
        {
            if (currentUsuarioUbigeo != null)
            {
                menuLlena();
            }
            else
                CargarData();
        }

        public void menuLlena()
        {
            var datoUsuario = usuMenuBL.GetAllFromUsuarioByPersonaFromLogin(currentUsuarioUbigeo.PERSONA.Dni);

            var permisos = (from p in usuMenuBL.GetAllUsuarioMenu(datoUsuario.UsuarioId)
                            select p.MENU.Tag.ToString()).ToArray();

            System.Windows.Forms.Button[] botones = new System.Windows.Forms.Button[] { 
            this.btnEliminar,this.btnEditar,this.btnNuevo};

            var noVisible = from nv in botones
                            where !permisos.Contains(nv.Tag.ToString())
                            select nv;

            noVisible.ToList().ForEach(nv => nv.Enabled = false);

            CargarData();
        }

        private void CargarData()
        {
            uBIGEOBindingSource.DataSource = UbigeoBL.GetAllFromUbigeos();

        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            var NewUbigeo = new frmMttUbigeo();
            NewUbigeo.AddUbigeo();
            NewUbigeo.ShowDialog();
            CargarData();

        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            UBIGEO ubigeo = uBIGEOBindingSource.Current as UBIGEO;

            if (ubigeo != null)
            {
                frmMttUbigeo frmMttoUbigeo = new frmMttUbigeo();
                frmMttoUbigeo._ubigeo = ubigeo;
                frmMttoUbigeo.UpdateUbigeo();
                frmMttoUbigeo.ShowDialog();
                CargarData();
            }
            else
            {
                MessageBox.Show("Debe de seleccionar un Ubigeo");
            }
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            var ubigeo = uBIGEOBindingSource.Current as UBIGEO;

            if (ubigeo != null)
            {
                var rpt = MessageBox.Show(this,
                    string.Format("¿Desea Eliminar el Ubigeo: " + ubigeo.IdUbigeo + "?" + " \n\n Departamento:  {0}", ubigeo.Departamento + "\n Provincia: " + ubigeo.Provincia + "\n Distrito: " + ubigeo.Distrito),
                    "Mensaje",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);

                if (rpt == DialogResult.Yes)
                {
                    UbigeoBL.RemoveUbiegeo(ubigeo.numero);
                    CargarData();
                }
            }
            else
            {
                MessageBox.Show("Debe de seleccionar un Ubigeo");
            }
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            var criterio = txtCriterio.Text == string.Empty ? null : txtCriterio.Text;
            uBIGEOBindingSource.DataSource = UbigeoBL.GetFromUbigeoByCriterio(criterio);
        }


        private void txtCriterio_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                var criterio = txtCriterio.Text == string.Empty ? null : txtCriterio.Text;
                uBIGEOBindingSource.DataSource = UbigeoBL.GetFromUbigeoByCriterio(criterio);
            }
        }

        private void uBIGEODataGridView_DoubleClick(object sender, EventArgs e)
        {
            if (isExtend == true)
            {
                _currentUbigeo = uBIGEOBindingSource.Current as UBIGEO;
                Close();
            }
        }

    }
}
